<?php
/*
 * ebiz front
 *
 * Customer Model
 *
 * @author li.jf
 *
 */

require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class OrderPackageItemDao extends BaseDao
{
    private $tableName = "order_package_items";

    public function __construct($dbUtil){
        parent::__construct($dbUtil);
    }

    //get productitemsinfo from the same order_package_items
    public function getOrderItemsInfoInOneOrderPackage($orderpackageId){
    	$sql="select opi.id,opi.quantity,oi.sale_price,oi.deal_price,pi.id as pid,pi.attribute_1,pi.value_1,
                pi.attribute_2,pi.value_2,pi.attribute_3,pi.value_3,pi.attribute_4,pi.value_4,pi.attribute_5,pi.value_5,p.title,p.url_title
    			from $this->tableName           opi
 				left outer join order_items 	oi 		on opi.order_item_id = oi.id
 				left outer join product_items 	pi 		on oi.item_id		 = pi.id
 				left outer join products 		p 		on p.id 			 = pi.product_id
 				where opi.order_package_id= ? and opi.is_delete = 0";
    	$param = array($orderpackageId);
    	return $this->dbUtil->SelectData($sql, $param);
    }

    /**
     * Get data from order_package_items, include order_items
     *
     * @param int $orderid
     * @return array
     */
    public function getPackageItemsByOrderId($orderid){
        $sql = "
            SELECT
                `p`.`title`,`p`.`url_title`, `pi`.`id` AS `product_item_id`, `oi`.`upc`, `oi`.`item_name`,
                `oi`.`quantity`,`oi`.`deal_price`,`oi`.`sale_price`,
                `op`.`id` AS `package_id`,`op`.`tracking_num`,`op`.`shipping_method_id`,
                `oi`.`discount`, `oi`.`ship_profit`, `oi`.`cheque_rule`, `oi`.`tax`, `oi`.`cheque_status`
            FROM `order_items` AS `oi`
                LEFT JOIN `order_package_items` AS `opi` ON `opi`.`order_item_id` = `oi`.`id` AND `opi`.`is_delete` = '0'
                LEFT JOIN `orders` AS `o` ON `oi`.`order_id` = `o`.`id`
                LEFT JOIN `order_packages` AS `op` ON `op`.`id` = `opi`.`order_package_id` AND `op`.`is_delete` = '0'
                LEFT JOIN `product_items` AS `pi` ON `oi`.`item_id` = `pi`.`id`
                LEFT JOIN `products` AS `p` ON `pi`.`product_id` = `p`.`id`
            WHERE `o`.`id` = ? AND `oi`.`is_delete` = '0'
            GROUP BY `oi`.`id`
            ORDER BY `oi`.`created`
        ";
        return $this->dbUtil->SelectData($sql, array($orderid));
    }

}